package gjbsh.bangong.ds.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import gjbsh.bangong.ds.service.权限模块Service;
import gjbsh.support.AppGlobalConfig;
import gjbsh.support.JsonResult;
import gjbsh.support.LoginUser;

/**
 * @author yaolongjun
 * @date   2020年5月22日 上午9:59:38
 */
@RestController
@RequestMapping("quanxian_moudle")
public class 权限模块Controller {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	权限模块Service ddService;
	
	/**
	 * @author yaolongjun
	 * @description	根据用户ID查询权限模块 
	 */
	@RequestMapping("find_moudle_list_by_userid")
	public JsonResult<?> find_moudle_list_by_userid(@RequestParam("userid")String ppUserId,
			HttpServletRequest request){
		try {
			if("".equals(ppUserId)) {
				return JsonResult.getErrorResult("用户ID不能为空");
			}
			
			// 查询当前登录用户ID
			LoginUser loginUser = (LoginUser)request.getAttribute(AppGlobalConfig.REQUEST_LOGINUSER_NAME);
			if(loginUser == null) {
				return JsonResult.getErrorResult("无登录信息");
			}
			
			String mmCurrentUserId = loginUser.getUserId() == null ? "" : loginUser.getUserId();
			if("".equals(mmCurrentUserId)) {
				return JsonResult.getErrorResult("无登录信息");
			}
			
			// 根据用户ID查询权限模块 
			List<Map<String,Object>> mmMoudleList = ddService.findMoudleListByUserId(ppUserId,mmCurrentUserId);
			int mmRows = 0;
			int mmColspan = 0;
			List<Map<String,Object>> mmResultMoudleList = new ArrayList<Map<String,Object>>();
			List<Map<String,Object>> mmTempMoudleList = new ArrayList<Map<String,Object>>();
			// 便于前台排版
			for(int i = 0; i < mmMoudleList.size(); i++) {
				mmColspan = (i+1)%3;
				
				if(mmColspan == 1) {
					mmTempMoudleList = new ArrayList<Map<String,Object>>();
				}
				Map<String, Object> mmMoudleMap = mmMoudleList.get(i);
				if(mmMoudleMap != null) {
					mmTempMoudleList.add(mmMoudleMap);
				}
				
				if(mmColspan == 0 || i == mmMoudleList.size()-1) {
					mmRows = i+1;
					Map<String,Object> mmMap = new HashMap<String,Object>();
					mmMap.put("行数", mmRows);
					mmMap.put("模块", mmTempMoudleList);
					mmResultMoudleList.add(mmMap);
				}
			}
			
			return JsonResult.getSuccessResult(mmResultMoudleList);
		} catch (Exception e) {
			logger.error("权限模块Controller->find_moudle_list_by_userid: "+e.getMessage());
			return JsonResult.getErrorResult("根据用户ID查询权限模块失败");
		}
	}

}
